//
//  WBNetworkingManager+Extension.swift
//  WB
//
//  Created by macssss on 16/7/2.
//  Copyright © 2016年 macssss. All rights reserved.
//

import Foundation

//MARK: - 封装新浪微博网络请求方法
extension WBNetworkManager {
    
    //MARK: - 加载微博数据字典
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion:(List: [[String: AnyObject]]?, isSuccess: Bool)->()) -> () {
        
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"//"https://api.weibo.com/2/statuses/public_timeline.json"
 
        let params = ["since_id": "\(since_id)",
                      "max_id": "\(max_id > 0 ? max_id - 1 : 0) "]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in


            let result = json?["statuses"] as? [[String: AnyObject]]
            
            completion(List: result, isSuccess: isSuccess)
        }
    }
    
    //MARK: -  获取未更新的微博数据
    func unreadCount(completion:(count: Int)->()) {
        
        //守护uid 是不是有值
        guard let uid = userAccount.uid else {
            
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid" : uid ]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            
            let dict = json as? [String : AnyObject]
            let count = dict?["status"] as? Int
        
            completion(count: count ?? 0)
            
        }
        
    }
    
}

// MARK: - 用户信息
extension WBNetworkManager {
    
    /// 加载当前用户信息 - 用户登录后立即执行
    func loadUserInfo(completion: (dict: [String: AnyObject])->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let params = ["uid": uid]
        
        // 发起网络请求
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            // 完成回调
            completion(dict: (json as? [String: AnyObject]) ?? [:])
        }
    }
}


//MARK: -  OAth 登录相关方法————
extension WBNetworkManager {
    
    //MARK: - 加载Access Token 方法
    func loadAccessToken(code: String, completion: (isSuccess: Bool)->()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        
        let params = ["client_id": WBAppKey,
                      "client_secret": WBAppSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": WBRedirectURI]
        
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            
            print(json)
            
            
            self.userAccount.yy_modelSet(with: (json as? [String:AnyObject]) ?? [:])
            
            
            // 加载当前用户信息
            self.loadUserInfo(completion: { (dict) in
                // 使用用户信息字典设置用户账户信息(昵称和头像地址)
                self.userAccount.yy_modelSet(with: dict)
                
                // 保存模型
               self.userAccount.saveAccess()
                
                print(self.userAccount)
                
                // 用户信息加载完成再，完成回调
                completion(isSuccess: isSuccess)
            })
        
        }
        
    }
    
}






